using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace CollegeShop.Infrastructure.Entities.Repository
{
    [Table("T_Order")]
    public class T_Order : IEntity<T_Order>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string OrderNumber { get; set; }

        public int TotalAmount { get; set; }

        public decimal TotalMoney { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string BuyUser { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string LinkMan { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string LinkPhone { get; set; }

        public int DormitoryID { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(256)]
        public string DeliveryAddress { get; set; }
       
        [Column(TypeName = "text")]
        public string GoodsDescript { get; set; }

        public int Payway { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(512)]
        public string Remark { get; set; }

        public DateTime OrderTime { get; set; }

        public int OrderClient { get; set; }

        public int State { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string HandleUser { get; set; }

        public int SalesUnitID { get; set; }

        public DateTime HandleTime { get; set; }

        public int BuyWay { get; set; }

        public int BuyClient { get; set; }

        public int AnonymousID { get; set; }

        public override T_Order Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.ID == id);
        }

        public override T_Order Find(System.Linq.Expressions.Expression<Func<T_Order, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Order.FirstOrDefault(where);
                if (item != null)
                {
                    this.ID = item.ID;
                    this.OrderNumber = item.OrderNumber;
                    this.TotalAmount = item.TotalAmount;
                    this.TotalMoney = item.TotalMoney;
                    this.BuyUser = item.BuyUser;
                    this.LinkMan = item.LinkMan;
                    this.LinkPhone = item.LinkPhone;
                    this.DormitoryID = item.DormitoryID;
                    this.DeliveryAddress = item.DeliveryAddress;
                    this.GoodsDescript = item.GoodsDescript;
                    this.Payway = item.Payway;
                    this.Remark = item.Remark;
                    this.OrderTime = item.OrderTime;
                    this.OrderClient = item.OrderClient;
                    this.State = item.State;
                    this.HandleUser = item.HandleUser;
                    this.SalesUnitID = item.SalesUnitID;
                    this.HandleTime = item.HandleTime;
                    this.BuyWay = item.BuyWay;
                    this.BuyClient = item.BuyClient;
                    this.AnonymousID = item.AnonymousID;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Order, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Order.Any(where);
            }
        }
    }
}
